<?php
/**
 * AttachmentsWidget class file
 *
 * @author Joachim Werner <joachim.werner@diggin-data.de>
 */

/**
 * AttachmentsWidget 
 * 
 * @uses CWidget
 * @package 
 * @version 0.1
 * @copyright 2010 Adam OPEL GmbH
 * @author Joachim Werner <joachim.werner@diggin-data.de> 
 * @license (tbd)
 */
class AttachmentsWidget extends CWidget
{
    // {{{ *** Members ***
    /**
     * @var MyModel ActiveRecord instance
     * @access public
     */
    public $model;
    /**
     * @var boolean Show trash placeholder? TRUE=yes, FALSE=no
     * @access public
     */
    public $showTrash = false;
    /**
     * @var array HTML attributes for the menu's root container tag
     */
    public $htmlOptions=array();

    public $attachments;
    // }}} 
    // {{{ *** Methods ***
    // {{{ init
    /**
     * Initialises the widget
     * 
     * @access public
     * @return void
     */
    public function init()
    {
        $this->htmlOptions['id']=$this->getId();
        $cs=Yii::app()->getClientScript();
        $this->attachments = $this->getAttachments();
        if(count($this->attachments)>0 and $this->showTrash==true) {
            $cs->registerCoreScript('jquery');
            $cs->registerCoreScript('jquery-ui');
            $cs->registerScript('AttachmentsWidget#'.mktime(),"jQuery(\"ul.attachments li.draggable\").draggable();");
            $cs->registerScript('AttachmentsWidget#'.mktime()+1,"jQuery(\"#attachments_trash\").droppable({over: function(event, ui){jQuery(this).addClass(\"highlight\")},out: function(event, ui){jQuery(this).removeClass(\"highlight\")}, drop: function( event, ui ) {file=ui.draggable.attr(\"path\");jQuery.ajax({url:'".CController::createUrl('/site/deleteAttachment')."',type:'POST',data:'file='+file});ui.draggable.remove();if(jQuery(\"ul.attachments li\").length==1) jQuery(\"#attachments_trash\").remove();}});");
        }
    } // }}} 
    // {{{ run
    /**
     * Runs the widget
     * 
     * @access public
     * @return void
     */
    public function run()
    {
        $this->render('attachmentsWidget',array('model'=>$model,'attachments'=>$this->attachments,'showTrash'=>$this->showTrash,'htmlOptions'=>$this->htmlOptions));
    } // }}} 
    // {{{ getAttachments
    /**
     * Gets the file attachments for the model instance
     * 
     * @access public
     * @return mixed Indexed array of attachments, containing path and url keys
     */
    public function getAttachments()
    {
        $dir = Yii::app()->basePath . '/data/'.get_class($this->model).'/'.$this->model->primaryKey;
        if(!is_dir($dir))
            return array();
        $files = CFileHelper::findFiles($dir);
        $result = array();
        foreach($files as $file) {
            $file = array(
                'path'  => str_replace("\\","/",$file),
                'url'   => 'protected/data/'.get_class($this->model).'/'.$this->model->primaryKey.'/'.basename(str_replace("\\","/",$file))
            );
            $result[] = $file;
        }
        return $result;

    } // }}} 
    // }}} End Methods
}
/* vim:set ai sw=4 sts=4 et fdm=marker fdc=4: */ 
?>
